class Node(object):
    def __init__(self, data=-1, left=None, right=None):
        self.data = data
        self.lchild = left
        self.rchild = right

class BinaryTree(object):
    def __init__(self, root=None):
        self.root = root
        self.queue = [] #层次顺序建树

    def insert(self, data):
        node = Node(data)
        self.queue.append(node)
        if self.root is None:
            self.root = node
        else:   #根不为空则处理队头
            if self.queue[0].lchild is None:
                self.queue[0].lchild = node
            elif self.queue[0].rchild is None:
                self.queue[0].rchild = node
                self.queue.pop(0)

    def preorder(self, root:Node):
        if root is None: return
        print(root.data, end=' ')
        self.preorder(root.lchild)
        self.preorder(root.rchild)

    def inorder(self, root:Node):
        if root is None: return
        self.inorder(root.lchild)
        print(root.data, end=' ')
        self.inorder(root.rchild)

    def postorder(self, root:Node):
        if root is None: return
        self.postorder(root.lchild)
        self.postorder(root.rchild)
        print(root.data, end=' ')

if __name__ == '__main__':
    tree = BinaryTree()
    for i in range(1,11):
        tree.insert(i)
    tree.preorder(tree.root)
    print()
    tree.inorder(tree.root)
    print()
    tree.postorder(tree.root)
    print()